GBML manual / Maps

map_create

Makes an empty map that can hold up to capacity pairs. key_example and value_example are any values of the kinds it will hold - map_create(16, "", 0) is a map, text keys with number values - and are never put in the map. capacity must be a whole number written right there in the code, and a power of two (2, 4, 8, 16, 32...).

Syntax

map_create(capacity, key_example, value_example) -> map<K,V>

Arguments

NameType
capacityint
key_exampleelement
value_exampleelement

Returns

map<K,V>

Description

Makes an empty map that can hold up to capacity pairs. map_create(16, "", 0) is a map with text keys and whole-number values, with room for 16 pairs.

capacity is the most pairs the map will ever hold, and it has to be a power of two: 2, 4, 8, 16, 32 and so on, never 10 or 30. That rule comes from the container the Game Boy Advance builds a map out of, and it is the same whichever way you write the size. If you pick another number, the compiler tells you the next size up.

It also has to be a number you type, like 16, not a variable, because a GBA cannot hand out memory while a game runs.

key_example and value_example tell GBML what kinds go in the map. The first is any value of the kind the keys are, the second any value of the kind the values are: 0 for whole numbers, 0.0 for decimals, "" for text, true for yes-or-no. The map starts empty, and neither example is ever stored. They are only read to learn the kinds. A key cannot be a struct, so a struct cannot be the first example.

Write the examples as the kinds you want. map_create(16, "", 0) is a map of whole-number values, and map_set(scores, "ada", 1.5) is refused when the project is checked: a decimal does not fit in a whole-number place, and the message names the map_create line that made the map hold whole numbers. Use 0.0 as the value example for a map of decimals. A whole number goes into a map of decimals as itself.

It sets the size of the variable it is given to, so it goes on the right-hand side of the line that creates the variable: var scores = map_create(16, "", 0);.

Leave the size out and you get 8.

map_create(16, "", 0) means the same as var scores: map<string, int, 16> = {}; and the /// @capacity 16 comment. See list_create for the three ways side by side.

Example

var scores = map_create(16, "", 0);     // an empty map: text keys, whole-number values
map_set(scores, "ada", 120);
var ada = map_get(scores, "ada");       // 120
var known = map_has(scores, "bob");     // false

On the GBA

Compiles to an empty fixed-size hash map of `capacity` pairs, the same storage `/// @type {map<K,V>}` + `/// @capacity capacity` gives. A hash map only compiles with a power-of-two size (the container checks this when the game is built), so any other number is refused and the next power of two up is suggested. The key kind must be one that can be hashed. Leave the size out and a map holds 8. The size has to be a plain number because it becomes a C++ template parameter: the Game Boy Advance has no heap to hand out memory while the game runs, so every container is sized when the ROM is built (anything else is refused). It must be the whole right-hand side of a declaration, because that is the one place a size can be attached to a variable. The example is only read for its kind - it is never evaluated, never stored and never put in the container.

See also: map_setmap_getmap_copylist_createset_createMapsWriting a type out, instead of in a commentData containers